package com.jlastu.dlxy_androidapp.presenter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.util.Pair;


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.dlxy_androidapp.base.WeakRefHandler;
import com.jlastu.dlxy_androidapp.contract.LoginContract;

import com.jlastu.dlxy_androidapp.entity.Goods;
import com.jlastu.dlxy_androidapp.entity.Photo;
import com.jlastu.dlxy_androidapp.entity.User;
import com.jlastu.dlxy_androidapp.view.login.LoginActivity;

import java.util.List;
import java.util.Objects;


public class LoginPresenter  implements LoginContract.ILoginPresenter {

    private static final int WHAT_GET_USER = 0;
    private LoginContract.ILoginView mLoginView;
    private Context mContext;
    private final String TAG = "LoginPresenter";


    public LoginPresenter(LoginActivity loginActivity, Context context) {
        mLoginView = loginActivity;
        mContext = context;
    }

    @Override
    public void getUser(String username, String password) {
        final int[] verify = {0};

        final int[] id = {0};


        new Thread(new Runnable() {
            @SuppressLint("Range")
            @Override
            public void run() {
                //此处异步进行数据查询
                //Uri uri = Uri.parse("content://com.jlastu.znb.provider/goods/1");
                Log.d(TAG, "UserName: " + username);
                Uri uri = Uri.parse("content://com.jlastu.dlxy_androidapp.provider/username/" + username);
                Cursor cursor = mContext.getContentResolver().query(
                        uri, null, null, null);
                try {
                    if (cursor != null && cursor.moveToFirst()) {
                        do {
                            User user = new User();
                            String user_password = cursor.getString(cursor.getColumnIndex("user_password"));
                            int user_id = cursor.getInt(cursor.getColumnIndex("_id"));
                            if (Objects.equals(user_password, password)) {
                                verify[0] = 1;
                                id[0] = user_id;

                            } else {
                                verify[0] = 2;
                                id[0] = 0;
                            }
                        } while (cursor.moveToNext());
                    } else {
                        verify[0] = 3;
                        id[0] = 0;
                    }
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                }
                Pair<Integer, Integer> dataPair = new Pair<>(verify[0], id[0]);
                Message message = mHandler.obtainMessage(WHAT_GET_USER, dataPair);
//                Message message = Message.obtain();
//                message.obj = list[0];
//                message.what = WHAT_GET_USER;
                mHandler.sendMessage(message);
            }
        }).start();
    }

    Handler mHandler = new WeakRefHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int what = msg.what;
            switch (what) {
                case WHAT_GET_USER:
                    // 从消息中获取Pair对象
                    @SuppressWarnings("unchecked")
                    Pair<Integer, Integer> dataPair = (Pair<Integer, Integer>) msg.obj;
                    // 获取两个列表
                    int verify = dataPair.first;
                    int id = dataPair.second;
                    mLoginView.showVerify(verify, id);
//                    mLoginView.showVerify((int) msg.obj);
                    Log.d(TAG, "判断状态: " + verify);
                    break;
            }
            return false;
        }
    });


}
